package com.wt.ai.demo.config;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.wt.ai.demo.tool.WeatherTool;

@Configuration
public class SpringAIConfig {
    private static String SYSTEM_ROLE = """
            你是一个助手，请用中文回答问题。
            """;

    @Bean
    public ChatClient chatClient(
            ChatClient.Builder builder,
            Advisor simpleLogAdvisor,
            Advisor messageChatMemoryAdvisor,
            WeatherTool weatherTool) {
        return builder
                .defaultSystem(SYSTEM_ROLE)
                .defaultAdvisors(simpleLogAdvisor, messageChatMemoryAdvisor)
                .defaultTools(weatherTool)
                .build();
    }

    @Bean
    public Advisor simpleLogAdvisor() {
        return new SimpleLoggerAdvisor();
    }

    @Bean
    public ChatMemory chatMemory() {
        return new InMemoryChatMemory();
    }

    @Bean
    public Advisor messageChatMemoryAdvisor(ChatMemory chatMemory) {
        return new MessageChatMemoryAdvisor(chatMemory);
    }

    /**
     * 创建并返回一个VectorStore的Spring Bean实例。
     *
     * @param embeddingModel 向量模型
     */
    @Bean
    public VectorStore vectorStore(EmbeddingModel embeddingModel) {
        return SimpleVectorStore.builder(embeddingModel).build();
    }
}
